<?php

/**
 * template_generation actions.
 *
 * @package    master_directory
 * @subpackage template_generation
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class template_generationActions extends sfActions {
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	
    public function executeLoadCategoryChildren(sfWebRequest $request){
    	
    	$category_content_id = $request->getParameter("id");
    	
    	$this->select_name = $request->getParameter("name");
        	
    	$this->contents = Doctrine_Core::getTable ( 'CategoryContent' )->getContentChildren($category_content_id);
    	
    }	
	
	public function executeSearch(sfWebRequest $request){
		$path_info = $request->getPathInfoArray(); 
		$domain_name = $path_info["SERVER_NAME"];
		//$domain_name = "boletosautobus.com.mx";
		//$domain_name = "busticketsto.com";
		//$domain_name = "fly-business.com";
    	
		$directory = Doctrine_Core::getTable ( 'MasterDirectory' )->getByDomain($domain_name);
		
	  	$search_data = $directory->getSearchConfiguration();
  		
  		$search_template = $search_data[0]->getTemplate();
  		$template2level_ids = array();
		foreach ($search_template->getTemplate2Level() as $template2level){
			array_push($template2level_ids, $template2level->getId());
		}
  		
  		$template_content_url = Doctrine_Core::getTable ( 'TemplateContent' )->getTemplateContent($search_template->getId(), sfConfig::get('app_content_type_url'));
  		$url_content = $template_content_url->getTheContent();
  		
  		$matchPattern = "/#%#(.*?)#%#/";
		
		$variables_name = array();
		$variables_str = array();
		
		if (preg_match_all ( $matchPattern, $url_content, $matches )) {
			$variables_name = $matches [1];
			$variables_str = $matches[0];
		}

		$i = 0;
		foreach ($variables_name as $variable_name){
			$temp_var = str_replace(" ", "_", $variable_name);
			$var_slash = str_replace(".", "_", $temp_var);
			
			$cat_content_id =  $request->getParameter($var_slash);
			
			$category_content = Doctrine_Core::getTable('CategoryContent')->find($cat_content_id);
			
			if ($category_content != null){
				$url_content = str_replace($variables_str[$i], str_replace(" ", "-", trim($category_content->getName())), $url_content);
				$i++;			
			}
		
		}
		
		//$current_uri = substr($request->getUri(), 0, strrpos($request->getUri(), "template_generation")) . str_replace(",", "", trim($url_content))  . '.html';
		$current_uri = substr($request->getUri(), 0, strrpos($request->getUri(), "template_generation")) . DirectoryUtils::slugify($url_content)  . '.html';
		
		return $this->redirect($current_uri);
    	
    }    
    
	
	public function executeIndex(sfWebRequest $request) {
		
		$path_info = $request->getPathInfoArray(); 
		$domain_name = $path_info["SERVER_NAME"];
		//$domain_name = "boletosautobus.com.mx";
		//$domain_name = "busticketsto.com";
		//$domain_name = "fly-business.com";
		$url_complement = $path_info["REQUEST_URI"];
		//$domain_name = "CoutureIn.com";
		//$this->getUser()->setAttribute("comments", array());

		$url = substr($url_complement, strrpos($url_complement, '/') + 1, strlen($url_complement) - strrpos($url_complement, '/') - 6 );
		
		$directory = Doctrine_Core::getTable ( 'MasterDirectory' )->getByDomain($domain_name);
		
		
		
		if ($directory->getLanguageId() != null){
			
			//echo $directory->getLanguage()->getCulture(); exit();
			
		
			$this->getUser()->setCulture($directory->getLanguage()->getCulture());
		}
		
		//echo $this->getUser()->getCulture(); exit();
		
		$this->the_directory = $directory;
		
		$this->searchComponents = $this->getSearchComponents($directory);
		
		$this->show_comments = 0;
		
		$this->show_rating = 0;
		
		$this->comments = array();
		
		$this->comment_fields = array();
		
		$parent_directory = $directory;
		
		while($directory->getParentId() != null){
			$parent_directory = $directory->getMasterDirectory();
		}
		
		$this->analytics_script = $parent_directory->getAnalyticsScript();
		
		$templates = Doctrine_Core::getTable ( 'Template' )->retreiveActiveTemplatesByDirectory($directory->getId());
		
		$template_contents = array();
		
		$involved_categories = array();
		
		foreach ($templates as $template){
			$template_content_item = Doctrine_Core::getTable ( 'TemplateContent' )->getTemplateContent($template->getId(), sfConfig::get('app_content_type_url'));
			if ($template_content_item != null){
				array_push($template_contents, $template_content_item);
			}

		}
		
		//possible template contentes with involved variables
		$possible_template_contents = $this->filterTemplateContents($template_contents, $url);
		
		$template_content_found = false;
		
		$template_content_iterator = 0;
		//echo $template_content_found; exit();
		while(!$template_content_found && $template_content_iterator < count($possible_template_contents) ){
			//search the variables names in template2level and upper_level table to obtain corresponding level
			$current_template_content = $possible_template_contents[$template_content_iterator][0];
			$current_variables_names = $possible_template_contents[$template_content_iterator][1];
			$current_variables_values = $possible_template_contents[$template_content_iterator][2];
			
			$category_content_found = array();
			$url_variable_names = array();
			for($i = 0; $i < count($current_variables_names); $i++){
				$variable_name = $current_variables_names[$i];
				$level = null;
				
				$t2l = Doctrine_Core::getTable ( 'Template2Level' )->getTemplate2LevelByName ( $current_template_content->getTemplateId(), $variable_name); 
				if ( $t2l != null) {
					
					$level = $t2l->getLevel();
				}
				else{
					$template = Doctrine_Core::getTable('Template')->find($current_template_content->getTemplateId());
					
					$templates2level_ids = array();
					foreach($template->getTemplate2Level() as $template2level){
					 	array_push($templates2level_ids, $template2level->getId());
					}
					
					$upper_level = Doctrine_Core::getTable ( 'UpperLevel' )->getUpperLevelByName ( $templates2level_ids, $variable_name );
					if ($upper_level != null){
						$level = $upper_level->getLevel ();
					}
					else{
						$side_level = Doctrine_Core::getTable ( 'SideLevel' )->getSideLevelByName ( $templates2level_ids, $variable_name );
						$level = $side_level->getLevel ();	
					}
				}
				
				
				$the_table = Doctrine_Core::getTable ( 'Level' );
				
				$the_table->setTreePath();
				
				$the_table->findTreePath($level);
				
				$tree_path = $the_table->getTreePath();
				
				$category_content = Doctrine_Core::getTable('CategoryContent');
				
				$category_content->setContentArr();				
				
				$content = Doctrine_Core::getTable('CategoryContent')->getContentByTreePath($tree_path, DirectoryUtils::slugify($current_variables_values[$i]), $level->getCategoryId());	
				
				if ($content != null){
					array_push($category_content_found, $content);
					
				}
				
			}
			if (count($category_content_found) == count($current_variables_names) ){ //preguntar por el length de los contenidos guardados
				$template_content_found = true;
			}
			else{
				$template_content_iterator ++;
			}
		}
		
		
	//	if (!$template_content_found && $url == ""){
		if (!$template_content_found){
			if ($url != ""){
				return $this->redirect("http://" . $domain_name);
			}
			else{
				$home_template = Doctrine_Core::getTable ( 'Template' )->getHomeTemplateByDirectory($directory->getId());
				$current_template_content = Doctrine_Core::getTable ( 'TemplateContent' )->getTemplateContent ( $home_template->getId(), sfConfig::get('app_content_type_url'));
				$template_content_found = true;
			}
		}
		
		
		if ($template_content_found){
			//phase 2: steal underwear
			$the_template = $current_template_content->getTemplate();
			
			
			$template_variables_value[0] = ($url != "") ? $the_template : null;
			$template_variables_value[1] = ($url != "") ? $possible_template_contents[$template_content_iterator][1] : array(); //variables names
			
			$template_variables_value[2] = ($url != "") ? $category_content_found : array();
			
			
			$template_id = $the_template->getId();
			$this->template_id = $template_id;
			
			
			$this->show_comments = $the_template->getShowComments();
			
			$this->show_rating = $the_template->getShowRating();
			
			if ($the_template->getShowComments() == 1){
				$this->comment_fields = $the_template->getCommentField();
				$selected_comments = array();
				
				$this->comments = $the_template->getComment();

				$category_content_ids = array();
				
				
				
				//foreach ($template_variables_value[1] as $variable) {
				for($vi = 0; $vi < count($template_variables_value[1]); $vi++){
						array_push($category_content_ids, $template_variables_value[2][$vi]->getId());
				}
				
				
				foreach ($this->comments as $acomment){
					$comment_category_ids = array();
					$comments2catcontent = $acomment->getComment2categoryContent();
					foreach ($comments2catcontent as $comment2catcontent){
						array_push($comment_category_ids, $comment2catcontent->getCategoryContentId());	
					}
			
					$common_category_content_ids = array_intersect($category_content_ids, $comment_category_ids);
					
					if (count($common_category_content_ids) == count($category_content_ids) && count($category_content_ids) != 0){
						
							array_push($selected_comments, $acomment);	
						
					}
				}
				
				$this->category_content_ids = $category_content_ids;
				
				
				
				$this->comments =  Doctrine_Core::getTable ( 'Comment' )->sortComments($selected_comments, $the_template->getRatingSortingId());//array_reverse($selected_comments);
				
			}
			
			$content_types = Doctrine_Core::getTable ( 'ContentType' )->retreiveActiveContentTypes ();
			
			foreach ( $content_types as $content_type ) {
				if ($content_type->getId() != sfConfig::get('app_content_type_footer')){
					
				$template_contents = Doctrine_Core::getTable ( 'TemplateContent' )->getTemplateContent ( $template_id, $content_type->getId ());
				if ($template_contents != null){
				$the_content = $template_contents->getTheContent ();
				
				$variables_name = array ();
				$matchPattern = "/#%#(.*?)#%#/";
				if (preg_match_all ( $matchPattern, $the_content, $matches )) {
					$variables_name = $matches [1];
				}
				
				$variables_name = array_unique($variables_name);
				
				$the_content = ($url != "") ? $this->getContentValue($variables_name,  $template_variables_value, $the_template, $the_content) : $the_content;
				
				if ($content_type->getId() == sfConfig::get("app_content_type_title")){
					$this->title_content = $the_content;
				}
				else if ($content_type->getId() == sfConfig::get("app_content_type_body")){
					
						$listing_names = array ();
						
						$output_list = array();
						
						$matchPattern = "/%#%(.*?)%#%/";
						if (preg_match_all ( $matchPattern, $the_content, $matches )) {
							$url_content_names = array();
							$url_contents = $template_variables_value[2];
							foreach ($url_contents as $url_content) {
								array_push($url_content_names, $url_content->getName());
							}
							
							$listing_names = $matches [1];
							
							foreach ($listing_names as $listing_name) {
								
								$listing = Doctrine_Core::getTable('Listing')->retreiveListByName($directory->getId(), $listing_name);
								$listing_fields = $listing->getListField();
								foreach ($listing_fields as $listing_field){
									$field_content_names = array();
									$field_selection = Doctrine_Core::getTable('ListingSelection')->getByField($listing_field->getId());
									
									foreach ($field_selection as $selection) {
										$selection_listing_contents = Doctrine_Core::getTable('ListingSelection2categoryContent')->getByListingSelection($selection->getId());
										foreach ($selection_listing_contents as $selection_listing_content ){
											$selectionContent = Doctrine_Core::getTable('CategoryContent')->find($selection_listing_content->getCategoryContentId());
											array_push($field_content_names, $selectionContent->getName());
											while ($selectionContent->getParentId() != null){
												$selectionContent = Doctrine_Core::getTable('CategoryContent')->find($selectionContent->getParentId());
												array_push($field_content_names, $selectionContent->getName());
											}
										}
									}
	
									if(count(array_intersect($url_content_names, array_unique($field_content_names))) == count($url_content_names)){
										
										if ($output_list[$listing->getName()] == null){
											$field_arr = array();
											array_push($field_arr, $listing_field);
											$output_list[$listing->getName()] = $field_arr;
										}
										else{
											array_push($output_list[$listing->getName()], $listing_field);
										}
									}
								}
							}
							
							foreach ($listing_names as $listing_name) {
								if ($output_list[$listing_name] != null){
										$content_str = '<b>' . $listing_name .'</b>';
										
										$content_str .= '<ul class="listing">';
										
										foreach($output_list[$listing_name] as $list_field){
											$content_str .= '<li>' . $list_field->getName() . ': ' . $list_field->getValue() . '</li>';
										}
										
										$content_str .= "</ul>";
										
										$the_content = str_replace("%#%" . $listing_name . "%#%", $content_str, $the_content);									
								}else{
										$the_content = str_replace("%#%" . $listing_name . "%#%", " ", $the_content);
								}
							}
							
						}
					
						$this->body_content = $the_content;
				} 
				else if ($content_type->getId() == sfConfig::get("app_content_type_meta_desc")){
						$this->meta_description = $the_content;
										
				} 				
				else if ($content_type->getId() == sfConfig::get("app_content_type_meta_kw")){
						$this->meta_kw = $the_content;
										
				} 					
				
				}	
			}	
			else{
				
				$footer_url = array();
				$footer_template = $the_template->getFooter();
				/*
				if($footer_template != null){
				$template2level_ids = array();
				foreach ($the_template->getTemplate2Level() as $template2level){
					array_push($template2level_ids, $template2level->getId());
				}
				
					$template_url_content = Doctrine_Core::getTable('TemplateContent')->getTemplateContent($footer_template->getId(), sfConfig::get('app_content_type_url'));
				
					$url_pattern = $template_url_content->getTheContent();
					
					$the_template_url_variables = $template_variables_value[1];
					
					
					$the_template_url_variables_values = $template_variables_value[2];
					//replace fixed content
					for ($i = 0; $i < count($the_template_url_variables); $i++) {
						$url_pattern = str_replace("#%#".$the_template_url_variables[$i]."#%#", str_replace(" ", "-",  trim($the_template_url_variables_values[$i]->getName())), $url_pattern);
					}
					//$the_content = str_replace("#%#".$variable_name."#%#", $str, $the_content); 
					$matchPattern = "/#%#(.*?)#%#/";
					if (preg_match_all ( $matchPattern, $url_pattern, $matches3 )) {
						$footer_variables_name = $matches3 [1];
						
						//replace brothers
						for ($i = 0; $i < count($the_template_url_variables); $i++) {
							foreach ($footer_variables_name as $footer_variable_name) {
								if(strpos($footer_variable_name, $the_template_url_variables[$i]) !== false){
									$found_side_level = Doctrine_Core::getTable('SideLevel')->getSideLevelByName($template2level_ids, $footer_variable_name);
									if($found_side_level != null){
										$parent = $the_template_url_variables_values[$i]->getParent();
										$brothers = $parent->getLevel();
										$url_pattern = str_replace("#%#".$footer_variable_name[$i]."#%#", str_replace(" ", "-",  trim($brothers[$found_side_level->getPosition()])) , $url_pattern);
									}
								}
							}
							
						}
						
						//replace children || new involved category, each repleacement spits a url
						foreach ($footer_variables_name as $footer_variable_name) {
							if(strpos($url_pattern, $footer_variable_name) !== false){
								$footer_template2level_ids = array();
								foreach ($footer_template->getTemplate2Level() as $template2level){
									array_push($footer_template2level_ids, $template2level->getId());
								}
								$footer_variable_level = $this->locateLevel($footer_variable_name, $footer_template, $footer_template2level_ids);
								
								$parent_content = null;
								$found = false;
								$c = 0;
								while (!$found && $c < count($the_template_url_variables)){
								$template_variable_level = $this->locateLevel($the_template_url_variables[$c], $the_template, $template2level_ids);
									if ($footer_variable_level->getParentId() == $template_variable_level->getId()){
										$found = true;
										$parent_content = $the_template_url_variables_values[$c]; 
									}
									$c++;
								}
								
								if ($found){
									
									$children_content = Doctrine_Core::getTable('CategoryContent')->getContentChildren($parent_content->getId());
									foreach ($children_content as $child_content){
										$url = str_replace("#%#".$footer_variable_name."#%#", str_replace(" ", "-",  trim($child_content->getName())) , $url_pattern);
										array_push($footer_url, $url);
									}
									
								}
								else{
									
									$the_table = Doctrine_Core::getTable ( 'Level' );
				
									$the_table->setTreePath();
									
									$the_table->findTreePath($footer_variable_level);
									
									$outside_tree_path = $the_table->getTreePath();
									
									$category_content = Doctrine_Core::getTable('CategoryContent');
									
									$category_content->setContentArr();
									
									$contents = Doctrine_Core::getTable('CategoryContent')->getContentsByTreePath($outside_tree_path, $footer_variable_level->getCategoryId());
									
									foreach ($contents as $content) {
										$url = str_replace("#%#".$footer_variable_name."#%#", str_replace(" ", "-", trim($content->getName())), $url_pattern);
										array_push($footer_url, $url);
									}
								}
							}
						}
	

						
					}else{
						array_push($footer_url, $url_pattern);
					}	
					
				}*/
				$this->footer_url = $footer_url;
			}
			}
			//search content
			$this->categories = $directory->getCategory();
		}
	}
	
	public function getContents($template_contents, $url){
		
	}
	
	
	public function filterTemplateContents($template_contents, $url){
		
		
		$filtered_template_content = array();
			
		foreach ($template_contents as $template_content){
			$the_content = $template_content->getTheContent();
			$variables_str =  array();	

			$matchPattern = "/#%#(.*?)#%#/";
			
			$variables_name = array();
			$variables_str = array();
			
			if (preg_match_all ( $matchPattern, $the_content, $matches )) {
				$variables_name = $matches [1];
				$variables_str =  $matches [0];
			}
			
			$the_content = "/^" . trim($the_content) . "/i";
			
			foreach ($variables_str as $variable_str){
				$the_content = str_replace($variable_str, "(\w.*)", $the_content);
			}
			
			preg_match($the_content, $url, $matches2);
			
			
			$var_arr = array();
			
			$var_values = array();
			
			$var_arr[0] = $template_content;
			
			if ($matches2 != null && $matches2[0] == $url){
				//variables array
				
				$var_arr[1] = $variables_name; //variables names (origin.country)
				
				//variables values (united states) --recorrer desde pos 1 hasta el  length
				for($i = 1; $i < count($matches2); $i++){
					array_push($var_values, str_replace("-", " ", $matches2[$i]));
				}
				
				$var_arr[2] = $var_values; 
				
				array_push($filtered_template_content, $var_arr);
			}
		}
		
		return $filtered_template_content;
		
	}
	

	
	public function findInvolvedLevels($template_id, $variables_name) {
		$main_levels = array ();
		
		$template = Doctrine_Core::getTable('Template')->find($template_id);
		$templates2level_ids = array();
		foreach($templates2level as $template2level){
		 	array_push($templates2level_ids, $template2level->getId());
		}
		
		foreach ( $variables_name as $variable_name ) {
			$variable_arr = explode ( ".", $variable_name );
			$level = null;
			if (count ( $variable_arr ) == 2) {
				$level = Doctrine_Core::getTable ( 'Template2LevelTable' )->getTemplate2LevelByName ( $template_id, $variable_name )->getLevel ();
			}else{
				$level = Doctrine_Core::getTable ( 'UpperLevel' )->getUpperLevelByName ( $templates2level_ids, $variable_name )->getLevel ();
					
			}
				array_push ( $main_levels, $level );
		}
		
		return $main_levels;
	}
	
	public function findMainLevels($template_id, $variables_name) {
		$main_levels = array ();
		
		foreach ( $variables_name as $variable_name ) {
			$variable_arr = explode ( ".", $variable_name );
			if (count ( $variable_arr ) == 2) {
				$level = Doctrine_Core::getTable ( 'Template2LevelTable' )->getTemplate2LevelByName ( $template_id, $variable_name )->getLevel ();
				array_push ( $main_levels, $level );
			}
		}
		
		return $main_levels;
	}	
	
	
	function getVariablesAndContent($template_content){
		$the_content = $template_content->getTheContent();
		
		$variables_str =  array();			
			
		if (preg_match_all ( '!#%#(\w+)#%#!', $the_content, $matches )) {
			$variables_name = $matches [1];
		}

		
		
	}
	
	function getContentValue($variables_name, $template_variables_value, $the_template, $the_content){

		foreach ($variables_name as $variable_name){
			$str = "";
			//look for the varaibla in upper_level table
			$variable_name_without_desc = str_replace(".description", "", $variable_name);
			
			$temp_arr = explode(".", $variable_name_without_desc);
			$first_point = $temp_arr[0] . "." . $temp_arr[1];
			
	
			$variable_found = false;
			$index = 0; 
			$variable_value = null;
			while(!$variable_found && $index < count($template_variables_value[1])){
				if ($template_variables_value[1][$index] == $first_point){
					$variable_found = true;
					$variable_value = $template_variables_value[2][$index];
				}
				$index++;
			}
		
			//TODO: revisar contenido del if UPPER
			if(count($temp_arr) > 2){
				$templates2level_ids = array();
				foreach($the_template->getTemplate2Level() as $template2level){
				 	array_push($templates2level_ids, $template2level->getId());
				}
				
				$upper_level_variable =  Doctrine_Core::getTable ( 'UpperLevel' )->getUpperLevelByName ( $templates2level_ids, $variable_name_without_desc );
				//in
				if ($upper_level_variable != null){
					$parent_count = count($temp_arr) -2;
					while($parent_count > 0){
						$variable_value = $variable_value->getParent();
						$parent_count--;
					}
				}
				else{
					$side_level_variable =  Doctrine_Core::getTable ( 'SideLevel' )->getSideLevelByName ( $templates2level_ids, $variable_name_without_desc );
					$parent = $variable_value->getParent();
					$children = $parent->getCategoryContent();
					
					$variable_value = $children[$side_level_variable->getPosition()];
					
				}
			}
			$str = strpos($variable_name, ".description") == true ?  $variable_value->getDescription() : $variable_value->getName();
		
			$the_content = str_replace("#%#".$variable_name."#%#", $str, $the_content); 

	}		
		return $the_content;
	}
	
	
  	protected function locateLevel($main_variable_name, $template, $template2level_ids){
			//look for the variable in template2level
			$level = null;
			$templates2level = Doctrine_Core::getTable('Template2Level')->getTemplate2LevelByName($template->getId(), $main_variable_name);
			if ($templates2level != null){
				$level = $templates2level->getLevel();
			}
			else {
				//look for the variable in upper_level			
				$upper_level = Doctrine_Core::getTable ( 'UpperLevel' )->getUpperLevelByName($template2level_ids, $main_variable_name);
				if ($upper_level != null){
					$level = $upper_level->getLevel();
				}
				else{
					$level = Doctrine_Core::getTable('SideLevel')->getSideLevelByName($template2level_ids, $main_variable_name)->getLevel();	
				}		
			}
			return $level;
  	}
  	
	private function getSearchComponents($directory){
  		// TODO: check field name
  		$search_data = $directory->getSearchConfiguration();
  		
  		$search_template = $search_data[0]->getTemplate();
  		$template2level_ids = array();
		foreach ($search_template->getTemplate2Level() as $template2level){
			array_push($template2level_ids, $template2level->getId());
		}
  		
  		$template_content_url = Doctrine_Core::getTable ( 'TemplateContent' )->getTemplateContent($search_template->getId(), sfConfig::get('app_content_type_url'));
  		$url_content = $template_content_url->getTheContent();
  		
  		$matchPattern = "/#%#(.*?)#%#/";
		
		$variables_name = array();
		
		if (preg_match_all ( $matchPattern, $url_content, $matches )) {
			$variables_name = $matches [1];
		}
		
		$base_array = array();
		//creating first array
		foreach ($variables_name as $variable_name) {
			$pieces = explode(".", $variable_name);
			$category = Doctrine_Core::getTable('Category')->retreiveActiveCategoryByNameDirectory($directory->getId(), $pieces[0]);
			if(array_key_exists($category->getId(), $base_array)){
				array_push($base_array[$category->getId()], $variable_name);
			}else{
				$variable_names = array();
				array_push($variable_names, $variable_name);
				$base_array[$category->getId()] = $variable_names;
			}
		}
		
		//ordering variables from parent to son
		foreach ($base_array as $category_id => $variableNames) {
			usort($variableNames, array($this, 'parentFirst'));
			$base_array[$category_id] = $variableNames;
		}
		
		$final_array = array();
		//creating second array
		foreach ($base_array as $category_id => $variableNames) {
			$component_element = array();
			if($this->isCategoryInclusive($category_id, $search_data)){
				$level = $this->locateLevel($variableNames[0], $search_template, $template2level_ids);
				array_push($component_element, $level);
				array_push($component_element, $variableNames);
			}else{
				$level = $this->locateLevel($variableNames[count($variableNames) - 1], $search_template, $template2level_ids);
				$main_name = array();
				array_push($main_name, $variableNames[count($variableNames) - 1]);
				array_push($component_element, $level);
				array_push($component_element, $main_name);
			}
			
			array_push($final_array, $component_element);
		}
		
		return $final_array;
		
  	}
  	
	private function parentFirst($variable1, $variable2) {
		$pieces1 = explode(".", $variable1);
		$pieces2 = explode(".", $variable2);
    	return count($pieces2) - count($pieces1);
    }
    
    private function isCategoryInclusive($category_id, $search_data) {
    	$isCategoryInclusive = false;
    	$category_found = false;
    	$i = 0;
    	while (!$category_found && $i < count($search_data)){
    		if($search_data[$i]->getCategoryId() == $category_id){
    			$category_found = true;
    			// TODO: check field name
    			$isCategoryInclusive =  $search_data[$i]->getInclusive();
    		}
    		$i ++;
    	}
    	return $isCategoryInclusive;
    }

  	
}
